package cn.lkx.eduservice.controller;


import cn.lkx.commonutils.R;
import cn.lkx.eduservice.entity.EduCourse;
import cn.lkx.eduservice.entity.EduTeacher;
import cn.lkx.eduservice.entity.vo.CourseInfoVo;
import cn.lkx.eduservice.entity.vo.CoursePublishVo;
import cn.lkx.eduservice.entity.vo.CourseQuery;
import cn.lkx.eduservice.entity.vo.TeacherQuery;
import cn.lkx.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lkx
 * @since 2022-05-03
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    EduCourseService courseService;

    //添加课程信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String cid = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",cid);
    }

    //上一步返回修改课程信息
    //1、查询课程基本信息 根据id 作于修改的回显
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    //2、修改保存数据库方法
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据id查询到 最终发布课程的确认信息
    @GetMapping("getPublishInfo/{id}")
    public R getPublishInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishInfo(id);
        return R.ok().data("publishInfo",coursePublishVo);
    }
    //发布课程方法 只用修改课程 的状态status: Draft未发布， Normal已发布
    @PostMapping("publishCourse/{id}")  //根据id修改
    public R publishCourseById(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal"); //发布状态
        boolean update = courseService.updateById(eduCourse);
        if(update){
            return R.ok();
        }else return R.error();
    }

    //分页且多条件查询课程信息
    @PostMapping("/pageAndConditions/{current}/{limit}")
    public R pageFindConditions(@PathVariable long current,
                                @PathVariable long limit,
                                //将前端传来的参数 自动封装成CourseQuery 对象 (实际加上RequestBody较多：前端以JSON格式传来参数，Get请求没有请求体)
                                @RequestBody CourseQuery courseQuery){
        Page<EduCourse> page = new Page<>(current,limit);
        courseService.pageQueryConditions(page,courseQuery);

        long total = page.getTotal();
        List<EduCourse> courseList = page.getRecords();
        return R.ok().data("total",total).data("rows",courseList);
    }

    //根据ID删除课程
    //TODO 还需完善同时删除课程下的阿里云上的小节视频 （已完善）
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourseById(@PathVariable String courseId){
        boolean result = courseService.removeCourseById(courseId);
        if (result){
            return R.ok();
        }else return R.error();
    }
}

